﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace kpi.MasterManager
{
    public partial class _TempInsertUserKpiInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_InsertUserKpiInfo_Click(object sender, EventArgs e)
        {
            String[] NamesArray = TextBox_UserNames.Text.Split('\n', '\r');
            for (int i = 0; i < NamesArray.Length; i++)
            {
                //Label_DebugMsg.Text += NamesArray[i].ToString();
                if (NamesArray[i].ToString() != "")
                {
                    //Label_DebugMsg.Text += NamesArray[i].ToString();

                    //创建的Password无法登录
                    //SQLProcedure_CreateUser(NamesArray[i].ToString());

                    //asp_net_membership_CreateUser(NamesArray[i].ToString(), "123456");
                    doInsertUserKpiInfo(NamesArray[i].ToString());
                }
            }

            TextBox_UserNames.Text = "";
        }

        protected void doInsertUserKpiInfo(String UserName)
        {
            SqlConnection SqlConnection = GetSqlConnection(null);
            SqlConnection.Open();

            SqlCommand cmd = new SqlCommand(GetSqlCommand("insert", null), SqlConnection);
            SqlParameter[] SqlParameter = new SqlParameter[11];
            SqlParameter[0] = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
            SqlParameter[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
            SqlParameter[2] = new SqlParameter("@u_kpi", SqlDbType.VarChar, 2000);
            SqlParameter[3] = new SqlParameter("@u_kpi_std", SqlDbType.VarChar, 5000);
            SqlParameter[4] = new SqlParameter("@u_kpi_comm", SqlDbType.VarChar, 5000);
            SqlParameter[5] = new SqlParameter("@u_kpi_rat", SqlDbType.Int, 50);
            SqlParameter[6] = new SqlParameter("@u_kpi_fx", SqlDbType.VarChar, 5000);
            SqlParameter[7] = new SqlParameter("@u_kpi_rec", SqlDbType.Int, 20);
            SqlParameter[8] = new SqlParameter("@uM_kpi_rec", SqlDbType.Int, 20);
            SqlParameter[9] = new SqlParameter("@u_kpi_ref", SqlDbType.VarChar, 50000);
            SqlParameter[10] = new SqlParameter("@u_kpi_ins_datetime", SqlDbType.DateTime, 50);            

            SqlParameter[0].Value = Guid.NewGuid();
            SqlParameter[1].Value = UserName;
            SqlParameter[2].Value = "TextBox_u_kpi.Text.ToString()";
            SqlParameter[3].Value = "TextBox_u_kpi_std.Text.ToString()";
            SqlParameter[4].Value = "TextBox_u_kpi_comm.Text.ToString()";
            SqlParameter[5].Value = Int32.Parse("10");
            SqlParameter[6].Value = "TextBox_u_kpi_fx.Text.ToString()";            
            SqlParameter[7].Value = 0;            
            SqlParameter[8].Value = 0;
            SqlParameter[9].Value = "NULL";
            SqlParameter[10].Value = DateTime.Now;           

            for (int i = 0; i < SqlParameter.Length; i++)
            {
                cmd.Parameters.Add(SqlParameter[i]);
            }
            cmd.CommandType = CommandType.Text;           

            cmd.ExecuteNonQuery();            

            SqlConnection.Close();
        }
        protected String GetSqlCommand(String commandType, String UserName)
        {
            switch (commandType)
            {
                case "select":
                    return "SELECT [u_kpi] ,[u_kpi_std] ,[u_kpi_comm] ,[u_kpi_rat] ,[u_kpi_fx] ,[u_kpi_rec] ,[uM_kpi_rec] FROM [kpi_forms_db].[dbo].[u_kpi_table] WHERE [Username]='" + UserName + "'";

                case "insert":
                    return "INSERT INTO u_kpi_table (id, UserName, u_kpi, u_kpi_std, u_kpi_comm, u_kpi_rat, u_kpi_fx, u_kpi_rec, uM_kpi_rec, u_kpi_ref, u_kpi_ins_datetime) VALUES "
        + " (@id, @UserName, @u_kpi, @u_kpi_std, @u_kpi_comm, @u_kpi_rat, @u_kpi_fx, @u_kpi_rec, @uM_kpi_rec, @u_kpi_ref, @u_kpi_ins_datetime)";

                case "update":
                    return "UPDATE [u_kpi_table] SET [u_kpi_conf]=@u_kpi_conf WHERE [UserName]=@UserName";

                default:
                    return "";

            }
            //return "SELECT [UserName] ,[u_kpi] ,[u_kpi_std] ,[u_kpi_comm] ,[u_kpi_rat] ,[u_kpi_fx] ,[u_kpi_rec] ,[uM_kpi_rec] ,[u_kpi_ref] FROM [kpi_forms_db].[dbo].[u_kpi_table] WHERE [Username]='" + UserName + "'";
            //return "SELECT [u_kpi] ,[u_kpi_std] ,[u_kpi_comm] ,[u_kpi_rat] ,[u_kpi_fx] ,[u_kpi_rec] ,[uM_kpi_rec] ,[u_kpi_ref] FROM [kpi_forms_db].[dbo].[u_kpi_table] WHERE [Username]='" + UserName + "'";
        }

        protected SqlConnection GetSqlConnection(String SqlConnectionString)
        {
            SqlConnection SqlConnection = new SqlConnection(GetSqlConnectionString());
            return SqlConnection;
        }

        protected String GetSqlConnectionString()
        {
            return "Data Source=192.168.0.241;Initial Catalog=kpi_forms_db;Persist Security Info=True;User ID=sa;Password=My.SA";
        }
    }
}